﻿using GeoParser;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace GeoLibraryTest
{
    /// <summary>
    ///This is a test class for WaypointTest and is intended
    ///to contain all WaypointTest Unit Tests
    ///</summary>
	[TestClass()]
	public class WaypointTest
	{
		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		[TestMethod]
		public void WaypointConstructorTest()
		{
			// Arrange
			double latitude = 20F;
			double longitude = -30F;

			// Act
			Waypoint target = new Waypoint(latitude, longitude);

			// Assert
			Assert.IsNull(target.Name);
			Assert.IsNull(target.Description);
			Assert.AreEqual(latitude, target.Latitude);
			Assert.AreEqual(longitude, target.Longitude);
		}

		[TestMethod]
		public void WaypointConstructorTest1()
		{
			// Arrange
			string latitude = "N20";
			string longitude = "W30";

			// Act
			Waypoint target = new Waypoint(latitude, longitude);

			// Assert
			Assert.IsNull(target.Name);
			Assert.IsNull(target.Description);
			Assert.AreEqual(20F, target.Latitude);
			Assert.AreEqual(-30F, target.Longitude);
		}

		[TestMethod]
		public void WaypointConstructorTest2()
		{
			// Arrange
			double latitude = 20F;
			double longitude = -30F;
			string name = "Name";
			string description = "Description";

			// Act
			Waypoint target = new Waypoint(latitude, longitude, name, description);

			// Assert
			Assert.AreEqual(name, target.Name);
			Assert.AreEqual(description, target.Description);
			Assert.AreEqual(latitude, target.Latitude);
			Assert.AreEqual(longitude, target.Longitude);
		}

		[TestMethod]
		public void WaypointConstructorTest3()
		{
			// Arrange
			double latitude = 50;
			double longitude = 0;
			string name = "Name";
			string description = "Description";
			XElement element = new XElement("wpt",
				new XAttribute("lat", latitude),
				new XAttribute("lon", longitude),
				new XElement("name", name),
				new XElement("desc", description));

			// Act
			Waypoint target = new Waypoint(element);

			// Assert
			Assert.AreEqual(latitude, target.Latitude);
			Assert.AreEqual(longitude, target.Longitude);
			Assert.AreEqual(name, target.Name);
			Assert.AreEqual(description, target.Description);
		}

		[TestMethod]
		public void ToStringTest()
		{
			// Arrange
			Waypoint target = new Waypoint("10", "-20", "Name", "Description");
			string expected = "Name (Description)";

			// Act
			string actual = target.ToString();

			// Assert
			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void StartFromTest()
		{
			// Arrange
			IEnumerable<Waypoint> unsorted = new Waypoint[] {
				new Waypoint(5, 5, "a", ""),
				new Waypoint(15, 15, "b", ""),
				new Waypoint(5, 15, "c", "") };
			string name = "b";
			ICollection expected = new List<Waypoint>(new Waypoint[] {
				new Waypoint(15, 15, "b", ""),
				new Waypoint(5, 15, "c", ""),
				new Waypoint(5, 5, "a", "") });

			// Act
			ICollection actual = Waypoint.StartFrom(unsorted, name).ToList();

			// Assert
			CollectionAssert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void WaypointConstructorTest4()
		{
			// Arrange
			string latitude = "N20";
			string longitude = "W30";
			string name = "Name";
			string description = "Description";

			// Act
			Waypoint target = new Waypoint(latitude, longitude, name, description);

			// Assert
			Assert.AreEqual(name, target.Name);
			Assert.AreEqual(description, target.Description);
			Assert.AreEqual(20F, target.Latitude);
			Assert.AreEqual(-30F, target.Longitude);
		}

		[TestMethod]
		public void EqualsTest_True()
		{
			// Arrange
			Waypoint target = new Waypoint("10", "-20", "Name", "Description");
			Waypoint other = new Waypoint("10", "-20", "Name", "Description");
			bool expected = true;
	
			// Act
			bool actual = target.Equals(other);

			// Assert
			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void EqualsTest_False()
		{
			// Arrange
			Waypoint target = new Waypoint("10", "-20", "Name", "Description");
			Waypoint other = new Waypoint("10", "-20", "Name", "Description1");
			bool expected = false;

			// Act
			bool actual = target.Equals(other);

			// Assert
			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void CloneTest()
		{
			// Arrange
			Waypoint orig = new Waypoint("10", "-20", "Name", "Description");
			
			// Act
			Waypoint actual = Waypoint.Clone(orig);

			// Assert
			Assert.AreEqual(orig, actual);
			Assert.AreNotSame(orig, actual);
		}
	}
}